package edu.bw.service.impl;

import edu.bw.dao.ProductDao;
import edu.bw.po.Product;
import edu.bw.service.ProductService;
import edu.bw.utils.TransactionManager;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Setter
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private TransactionManager transactionManager;

    @Override
    public void updateProduct(Product product) {
/*        try {
            transactionManager.beginTransaction();
            productDao.updateProduct(product);
            transactionManager.commit();
        } catch (Exception e) {
            transactionManager.rollback();
            e.printStackTrace();
        } finally {
            transactionManager.release();
        }*/

        productDao.updateProduct(product);

    }

    @Override
    public void saveProduct(Product product) {
       /* try {
            transactionManager.beginTransaction();
            productDao.saveProduct(product);
            transactionManager.commit();
        } catch (Exception e) {
            transactionManager.rollback();
            e.printStackTrace();
        } finally {
            transactionManager.release();
        }*/
        productDao.saveProduct(product);
    }

    @Override
    public void deleteProduct(Integer pid) {
      /*  try {
            transactionManager.beginTransaction();
            productDao.deleteProduct(pid);
            transactionManager.commit();
        } catch (Exception e) {
            transactionManager.rollback();
            e.printStackTrace();
        } finally {
            transactionManager.release();
        }*/
        productDao.deleteProduct(pid);
    }

    @Override
    public List<Product> findAll() {
        return productDao.findAll();
    }
}
